package com.promote.threadPool;

import java.util.concurrent.*;

/**
 * @ClassName ThreadPoolExecutorDemo
 * Description 线程池demo
 * @Author LiZiHao
 * Date 2021/9/13 15:05
 * @Version 1.0
 **/
public class ThreadPoolExecutorDemo {
    private static final int CORE_POOL_SIZE = 5;
    private static final int MAX_POOL_SIZE = 10;
    private static final int QUEUE_CAPACITY = 100;
    private static final Long KEEP_ALIVE_TIME = 1L;

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                CORE_POOL_SIZE,
                MAX_POOL_SIZE,
                KEEP_ALIVE_TIME,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(QUEUE_CAPACITY),
                new ThreadPoolExecutor.CallerRunsPolicy());

        for (int i = 0; i < 10; i++) {
            MyRunnable myRunnable = new MyRunnable("" + i);
            threadPoolExecutor.execute(myRunnable);
            //Future<?> future = threadPoolExecutor.submit(myRunnable);
        }
        threadPoolExecutor.shutdown();
        while (!threadPoolExecutor.isTerminated()) {
        }
        System.out.println("Finished all threads");
    }


}
